package com.szgr.baoyin.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Cache;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.szgr.baoyin.MyApplication;
import com.szgr.baoyin.R;

import java.util.List;

/**
 * Created by yang on 15/9/24.
 */
public class AccessoriesGridAdapter extends BaseAdapter {
    private Context mContext;
    private List<String> urls;
    public AccessoriesGridAdapter(Context context,List<String> urls){
        mContext = context;
        this.urls = urls;
    }
    @Override
    public int getCount() {
        return urls.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if(convertView == null){
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_accessories_shop_grid_view,parent,false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        }
        holder = (ViewHolder) convertView.getTag();

        Cache cache = MyApplication.getmInstance().getRequestQueue().getCache();
        Cache.Entry entry = cache.get(urls.get(position));
        if(entry != null){
            holder.img_big.setImageBitmap(BitmapFactory.decodeByteArray(entry.data, 0, entry.data.length));
        }else{
            ImageLoader.ImageListener listener = ImageLoader.getImageListener(holder.img_big,R.drawable.home_page,R.drawable.home_page);

            MyApplication.getmInstance().getImageLoader().get(urls.get(position), listener);
        }
        return convertView;
    }

    private class ViewHolder {
        TextView tv_big;
        TextView tv_small;
        ImageView img_small;
        ImageView img_big;

        public ViewHolder(View view){
            tv_big = (TextView) view.findViewById(R.id.accessory_big_text);
            tv_small = (TextView) view.findViewById(R.id.accessory_small_text);
            img_small = (ImageView) view.findViewById(R.id.accessory_small_icon);
            img_big = (ImageView) view.findViewById(R.id.accessory_image_bottom);
        }
    }
}
